/*
* MIT License
*
* Copyright (c) 2017 wen.gu <454727014@qq.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.


*/

#include <string.h>

#include "tiny_parcel.h"
#include "general_error.h"

#define LOGD printf
#define LOGE printf


void parcel_test()
{
#define PARCEL_BUF_SIZE 1024

    char temp_buf[PARCEL_BUF_SIZE];

    char* pbuf1 = temp_buf;

    char* pbuf2 = NULL;
    tiny_parcel_t write_parcel;
    GS32 dataInt32 = 123;
    GS64 dataInt64 = 456;
    GFLT dataFloat = (GFLT)789.123;
    GSTR dataString = "this is string";
    GSTR dataData = "this is data"; /** fake pure data */
    GU32 dataLen = strlen(dataData);


    GS32 ret = tfParcelInitialize(&write_parcel, pbuf1, PARCEL_BUF_SIZE);

    if (ret == G_OK)
    {
        LOGD("write data:\n");
        LOGD("int32: %d\n", dataInt32);
        LOGD("int64: %lld\n", dataInt64);
        LOGD("float: %f\n", dataFloat);
        LOGD("string: %s\n", dataString);
        LOGD("pure data: %s\n", dataData);

        /** write data to parcel */
        tfParcelWriteInt32(&write_parcel, dataInt32);
        tfParcelWriteInt64(&write_parcel, dataInt64);
        tfParcelWriteFloat(&write_parcel, dataFloat);
        tfParcelWriteString(&write_parcel, dataString);
        ret = tfParcelWrite(&write_parcel, dataData, dataLen);

        if (ret == G_OK)
        {
            /** fake data transform */
            pbuf2 = pbuf1;
            LOGD("\n=======================================\n");
            /** read data from parcel */
            {/** fake other thread/porcessor/system */
                GS32 readInt32 = 0;
                GS64 readInt64 = 0;
                GFLT readFloat = 0.0;
                GSTR readString = NULL;
                GU32 readDataLen = dataLen;
                char readPureData[PARCEL_BUF_SIZE] = { 0 };
                tiny_parcel_t read_parcel;

                ret = tfParcelInitialize(&read_parcel, pbuf2, PARCEL_BUF_SIZE);

                if (ret == G_OK)
                {
                    readInt32 = tfParcelReadInt32(&read_parcel);
                    readInt64 = tfParcelReadInt64(&read_parcel);
                    readFloat = tfParcelReadFloat(&read_parcel);
                    readString = tfParcelReadString(&read_parcel);

                    ret = tfParcelRead(&read_parcel, readPureData, readDataLen);

                    if (ret == G_OK)
                    {
                        LOGD("read data:\n");
                        LOGD("int32: %d\n", readInt32);
                        LOGD("int64: %lld\n", readInt64);
                        LOGD("float: %f\n", readFloat);
                        LOGD("string: %s\n", readString);
                        LOGD("pure data: %s\n", readPureData);

                        LOGD("\n=========================================\n");
                        if ((dataInt32 == readInt32) &&
                            (dataInt64 == readInt64) &&
                            (dataFloat == readFloat) &&
                            (!strcmp(dataString, readString)) &&
                            (!memcmp(dataData, readPureData, dataLen)))
                        {
                            LOGD("data check OK ...\n");
                        }
                        else
                        {
                            LOGD("data check failed ...\n");
                        }
                    }
                    else
                    {
                        LOGE("read pure data from parcel failed(0x%08x)\n", ret);
                    }
                }
                else
                {
                    LOGE("initialize read parcel failed(0x%08x)\n", ret);
                }

            }
        }
        else
        {
            LOGE("read pure data to parcel failed(0x%08x)\n", ret);
        }

    }
    else
    {
        LOGE("initialize write parcel failed(0x%08x)\n", ret);
    }
}